;Make a DM, with31 actuators. Numbering starts from top left.

function make_dm31, posns, psize, m_pix, projmat=projmat

if (n_elements(posns) ne 19) then return, -1
p = posns - mean(posns)
edge = 0.5*(p[7:18] + shift(p[7:18],-1))
q = [p,edge]
x = fltarr(19)
y = fltarr(19)
sep =0.2/m_pix
angles =( 60*findgen(6))*!pi/180.
for i=0,5 do x[i+1]= sep*cos(angles[i])
for i=0,5 do y[i+1]= sep*sin(angles[i])
angles =( 30*findgen(12))*!pi/180.
for i=0,11 do x[i+7]= 2*sep*cos(angles[i])
for i=0,11 do y[i+7] =2*sep*sin(angles[i])
if (keyword_set(projmat)) then begin
 tfa_project = [[x/sep],[y/sep],[(x^2+y^2)/sep^2], [(x^2-y^2)/sep^2], [x*y/sep^2]]
 nv = (size(tfa_project))[2]
 norm = sqrt(total(tfa_project^2,1))
 norm = transpose(rebin(norm,nv,19))
 tfa_onorm = tfa_project
 for i=1,nv-1 do begin
     for j=0,i-1 do tfa_onorm[*,i] -= total(tfa_onorm[*,j]*tfa_project[*,i])*tfa_onorm[*,j]
 endfor
 norm = sqrt(total(tfa_onorm^2,1))
 norm = transpose(rebin(norm,5,19))
 tfa_onorm /= norm
 projmat = tfa_onorm # transpose(tfa_onorm)
end
x = [x,replicate(0,12)]
y = [y,replicate(0,12)]
angles += 15*!pi/180.
for i=0,11 do x[i+19] = sep*3.4*cos(angles[i])
for i=0,11 do y[i+19] = sep*3.4*sin(angles[i])


x += psize/2
y += psize/2
triangulate,x, y, triangles
dm = trigrid(x,y,q,triangles,[1,1], [0,0,psize-1,psize-1])
return, dm
end
